% -----------------------
% Project: Secret and Overt Information Acquisition in Financial Markets
% This code produces Panels b and c of Figure 3
% Site visit: Duopoly case
% -----------------------

%% Large c/sigma_u: Panel b1
clear;clc;
J=2;
c=0.5;Su=1;
options = optimset('Display','off', 'TolFun', 1e-15,'TolX',1e-15); % Turn off Display
tolfval=1e-10;

HHbar=linspace(0,0.4,50);
Hvol=NaN(size(HHbar));
Hmand1=NaN(size(HHbar));
Hmand2=NaN(size(HHbar));

   % overt: ho
    fun = @(h) ( (h*(1+h)*(2+h)^2*(2+h+h*J)^4)/...
                 ((6*J^2-J-3)*h^2+(4*J^2+10*J-8)*h+8*J-4 )^2- Su^2/(4*J^3*c^2) )^2;
    h0 = 1; A = [-1]; bb = [0];
    Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
    [ho,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options);      
   % secret: hs
    fun = @(h) ( h*(1+h)*(2+h+h*J)^2 - Su^2/(J*c^2) )^2;
    h0 = 1; A = [-1]; bb = [0];
    Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
    [hs,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options); 
    
param=[J,c,Su];

for i=1:length(HHbar)
    hbar=HHbar(i);
   %================================ 
   % 1. Mandatory : Oligopoly: large c
    if hbar<=hs
        heqm=hbar-1e-06;
        fun = @(hj) ((-1).*c+(2+heqm).^2.*(4+(4+heqm).*hj+heqm.*(2+hj).*J).^(-1).*(4.* ...
                  heqm.*(1+heqm).*((-1)+J)+hj.*(4+(-3).*heqm.^2+4.*heqm.*(1+heqm).* ...
                  J)+hj.^2.*(4+heqm.*(3+J+heqm.*J))).^(-1/2).*Su...
                  )^2;
        h0 = hs; A = [-1]; bb = [hbar];
        Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
        [hjb,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options);   
        
        pieqm1=pij(heqm,heqm,heqm,param);
        pidev=pij(hjb,hjb,heqm,param);
        if pieqm1>=pidev
            Hmand1(i)=heqm;
        end
        
        pieqm2=pij(hs,hs,hs,param);
        pidev=pij(hbar-1e-06,hbar-1e-06,hs,param);
        if pieqm2>=pidev
            Hmand2(i)=hs;
        end        
    else
        pieqm1=pij(hs,hs,hs,param);
        pidev=pij(hbar,hbar,hs,param);
        if pieqm1>=pidev
            Hmand2(i)=hs;
        end
    end
    
   %================================    
    % 2. Voluntary : Monopoly
    Hvol(i)=hs;
end

figure(11);
plt=plot(HHbar,Hvol,'b-',HHbar,Hmand1,'r--o',HHbar,Hmand2,'r--o','LineWidth',1); hold on;
legend('Voluntary','Mandatory','Interpreter','latex', 'AutoUpdate', 'off'); legend box off;
plot(HHbar,Hvol,'b-','LineWidth',1.8);
ylim([0,HHbar(end)]);
xlabel('$\bar{h}$','Interpreter','latex'); 
ylabel('$h^*$','Interpreter','latex');
   box off; set(gca,'FontSize',22); set(gca,'TickLabelInterpreter','latex');
set(gcf, 'Position',  [100, -100, 670, 620]); 

x1 = xline(hs,'-.','$h_s$','DisplayName','hs','Interpreter','latex');
set(x1,'FontSize',20,'LineWidth',0.5,'Color', [0.5 0.5 0.5]);
set(x1,'LabelVerticalAlignment','bottom','LabelHorizontalAlignment','center'...
    ,'LabelOrientation','horizontal');
x2 = xline(ho,'-.','$h_o$','DisplayName','ho','Interpreter','latex');
set(x2,'FontSize',20,'LineWidth',0.5,'Color', [0.5 0.5 0.5]);
set(x2,'LabelVerticalAlignment','bottom','LabelHorizontalAlignment','center'...
    ,'LabelOrientation','horizontal');
text(-0.03,0.23,'$h_s$',...
    'Interpreter','latex','fontsize', 22);




%% Small c/sigma_u: Panel b2

clear;clc;
J=2;
c=0.015;Su=1;
options = optimset('Display','off', 'TolFun', 1e-15,'TolX',1e-15); % Turn off Display
tolfval=1e-10;

HHbar=linspace(0,6,50);
Hvol1=NaN(size(HHbar));
Hvol2=NaN(size(HHbar));
Hmand1=NaN(size(HHbar));
Hmand2=NaN(size(HHbar));
CheckBelief=NaN(size(HHbar));

   % overt: ho
    fun = @(h) ( (h*(1+h)*(2+h)^2*(2+h+h*J)^4)/...
                 ((6*J^2-J-3)*h^2+(4*J^2+10*J-8)*h+8*J-4 )^2- Su^2/(4*J^3*c^2) )^2;
    h0 = 1; A = [-1]; bb = [0];
    Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
    [ho,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options);      
   % secret: hs
    fun = @(h) ( h*(1+h)*(2+h+h*J)^2 - Su^2/(J*c^2) )^2;
    h0 = 1; A = [-1]; bb = [0];
    Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
    [hs,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options);   
param=[J,c,Su];

for i=1:length(HHbar)
    hbar=HHbar(i);
 
   %================================ 
   % 1. Mandatory : Oligopol: small c
    if hbar<=hs
        heqm=hs;
        fun = @(hj) ((-1).*c+(2+heqm).^2.*(4+(4+heqm).*hj+heqm.*(2+hj).*J).^(-1).*(4.* ...
                  heqm.*(1+heqm).*((-1)+J)+hj.*(4+(-3).*heqm.^2+4.*heqm.*(1+heqm).* ...
                  J)+hj.^2.*(4+heqm.*(3+J+heqm.*J))).^(-1/2).*Su...
                  )^2;
        h0 = hbar; A = [1]; bb = [hbar];
        Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
        [hjb,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options);   
        CheckBelief(i)=hjb;
        
        pieqm=pij(heqm,heqm,heqm,param);
        pidev=pij(hjb,hjb,heqm,param);
        if pieqm>=pidev
            Hmand1(i)=hs;
        end
              
    else
        pieqm1=pij(hs,hs,hs,param);
        pidev=pij(hbar,hbar,hs,param);
        if pieqm1>=pidev
            Hmand1(i)=hs;
        end
        
        heqm=hbar;
        fun = @(hj) ((-1).*c+(2+heqm).^2.*(4+(4+heqm).*hj+heqm.*(2+hj).*J).^(-1).*(4.* ...
                  heqm.*(1+heqm).*((-1)+J)+hj.*(4+(-3).*heqm.^2+4.*heqm.*(1+heqm).* ...
                  J)+hj.^2.*(4+heqm.*(3+J+heqm.*J))).^(-1/2).*Su...
                  )^2;
        h0 = hbar; A = [1]; bb = [hbar];
        Aeq = []; beq = []; lb = []; ub = []; nonlcon = [];
        [hjb,~] = fmincon(fun,h0,A,bb,Aeq,beq,lb,ub,nonlcon,options);   
        
        pieqm=pij(heqm,heqm,heqm,param);
        pidev=pij(hjb,hjb,heqm,param);
        if pieqm>=pidev
            Hmand2(i)=hbar;
        end        
    end
    
   %================================    
    % 2. Voluntary : Monopoly
    Hvol1(i)=Hmand1(i)-1e-02; %to avoid overlap
    Hvol2(i)=Hmand2(i)-1e-02;
    
end


figure(12);
plot(HHbar,Hvol1,'b-',HHbar,Hmand1,'r--o',HHbar,Hvol2,'b-',HHbar,Hmand2,'r--o',HHbar,Hmand1,'r--o','LineWidth',1); hold on;
% legend('Mandatory','Voluntary','Interpreter','latex', 'AutoUpdate', 'off'); legend box off;
plot(HHbar,Hvol1,'b-',HHbar,Hvol2,'b-','LineWidth',1.8);
ylim([0,HHbar(end)]);
xlim([0,HHbar(end)]);
xlabel('$\bar{h}$','Interpreter','latex'); 
ylabel('$h^*$','Interpreter','latex');
   box off; set(gca,'FontSize',22); set(gca,'TickLabelInterpreter','latex');
yticks([0 2 4 6])
yticklabels({'0','2','4','6'})
set(gcf, 'Position',  [100, -100, 670, 620]); 


fill=patch([hs 4.53 4.53 hs],...
    [0 0, 6 6], [0.9608 0.8705 0.7020]...
    ,'EdgeColor','none','FaceAlpha',0.2);

x1 = xline(hs,'-.','$h_s$','DisplayName','hs','Interpreter','latex');
set(x1,'FontSize',20,'LineWidth',0.5,'Color', [0.5 0.5 0.5]);
set(x1,'LabelVerticalAlignment','bottom','LabelHorizontalAlignment','center'...
    ,'LabelOrientation','horizontal');
x2 = xline(ho,'-.','$h_o$','DisplayName','ho','Interpreter','latex');
set(x2,'FontSize',20,'LineWidth',0.5,'Color', [0.5 0.5 0.5]);
set(x2,'LabelVerticalAlignment','bottom','LabelHorizontalAlignment','center'...
    ,'LabelOrientation','horizontal');
text(-0.4,3.45,'$h_s$',...
    'Interpreter','latex','fontsize', 22);


